package com.kotei.config;

import com.baomidou.mybatisplus.core.MybatisSqlSessionFactoryBuilder;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;

import javax.sql.DataSource;

public class MyBatisPlusConfig {
    
    public static SqlSessionFactory getSqlSessionFactory() {
        // 1. 配置数据源
        HikariConfig hikariConfig = new HikariConfig();
        hikariConfig.setJdbcUrl("jdbc:mysql://localhost:3307/nfobs");
        hikariConfig.setUsername("root");
        hikariConfig.setPassword("kotei$88");
        hikariConfig.setDriverClassName("com.mysql.cj.jdbc.Driver");
        DataSource dataSource = new HikariDataSource(hikariConfig);
        
        // 2. 配置事务
        TransactionFactory transactionFactory = new JdbcTransactionFactory();
        
        // 3. 创建环境
        Environment environment = new Environment("development", transactionFactory, dataSource);

        // 4. 创建配置
        Configuration configuration = new Configuration(environment);
        
        // 5. 添加Mapper接口（可以指定包名或具体类）
        configuration.addMappers("com.baomidou.mybatisplus.core.mapper");
        configuration.addMappers("com.kotei.mapper");

        // 6. 使用MyBatis-Plus的SqlSessionFactoryBuilder
        return new MybatisSqlSessionFactoryBuilder().build(configuration);
    }
}